# -*- coding: utf-8 -*-
__author__ = 'xujianhua'
#控制命令相关服务器的子线程

import threading, time,simplejson
#import Server

json_cmd={
    "cmd":1,
    "phi":[],
    "model":0,
    "filename":[],
    "resp_code":0
}


class TcpThread:

    def __init__(self,tcp,backqueue):#backqueue将每个线程收到的数据反向传给总的控制进程
        self.backqueue = backqueue
        self.tcp = tcp
        self.run = True
        self.tcp_data = threading.Thread(target = self.wait_data)
        self.tcp_data.start()

    def wait_data(self):
        try:
            while(self.run):
                try:
                    data = self.tcp.recv(512)
                    if data == '':
                        return
                    print "data",data

                    try:
                        cmd_json = simplejson.loads(data)
                        self.backqueue.put({"client":self.tcp,"cmd":cmd_json})
                        # if(cmd_json['opType'] == 1):
                        #     pass
                        # elif(cmd_json['opType'] == 2):
                        #     pass
                        # elif(cmd_json['opType'] == 3):
                        #     pass
                        # elif (cmd_json['opType'] == 4):
                        #     pass
                    except Exception,e:
                        print "json loads error ",e
                except:
                    return

        except KeyboardInterrupt:
            self.tcp.close()
            return

    def close(self):
        self.run = False
